package com.sniff.atp.service.order;

import com.alibaba.fastjson2.JSONObject;
import com.sniff.atp.constant.RedisPrefix;
import com.sniff.atp.dto.ContentDTO;
import com.sniff.atp.manage.RedisManage;
import com.sniff.atp.manage.XXLManage;
import com.sniff.atp.utils.DBUtil;
import com.sniff.atp.utils.DateUtil;
import com.sniff.atp.utils.LogUtil;
import com.sniff.atp.utils.ToolsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class PurchaseAllotOrder {
    @Autowired
    private RedisManage redisManage;

    @Autowired
    private XXLManage xxlManage;
    @Value("${sniff.task.purchaseOrderId}")
    private String purchaseOrderId;

    @Value("${sniff.task.purchaseBuyerId}")
    private String purchaseBuyerId;


    public void allotAutoOrder() {
        redisManage.delKey(RedisPrefix.SNIFF_PURCHASE_ORDER + DateUtil.dateToDay(new Date()));
        xxlManage.runTask(purchaseOrderId);
        ToolsUtil.sleep(5000);
        xxlManage.runTask(purchaseBuyerId);
        redisManage.delKey(RedisPrefix.SNIFF_PURCHASE_ORDER + DateUtil.dateToDay(new Date()));
    }

    /**
     * 根据订单查询采购单号
     *
     * @param platformOrderNo
     * @return
     */
    public String getPurchaseOrderNo(String platformOrderNo) {
        // 查询成品采购单
        List<JSONObject> purchaselist = DBUtil.select("Purchase-查询成品采购单号", new ContentDTO("", platformOrderNo));
        // 查询oem采购单
        List<JSONObject> oemPurchaselist = DBUtil.select("Purchase-查询OEM采购单号", new ContentDTO("", platformOrderNo));

        ArrayList<String> arrayList = new ArrayList<>();
        String wareHouse = "";

        for (JSONObject ob : purchaselist) {
            String purchase_no = ob.getString("purchase_no");
            arrayList.add(purchase_no);
            wareHouse = ob.getString("ware_code");
        }

        for (JSONObject ob : oemPurchaselist) {
            String purchase_no = ob.getString("oem_purchase_no");
            arrayList.add(purchase_no);
            wareHouse = ob.getString("ware_code");
        }

        JSONObject data = new JSONObject();
        data.put("purchaseNo", arrayList);
        data.put("wareHouse", wareHouse);
        // 没有查到采购单号报错，中断流程
        if (arrayList.isEmpty()) {
            LogUtil.warn("没有查到对应的采购单号！！！");
            return "{}";
        }

        LogUtil.warn("【采购】获取采购单号：" + data.toJSONString());



        return data.toJSONString();
    }

    /**
     * 根据订单查询采购单号，采购单状态，仓库
     *
     * @param platformOrderNo
     * @return
     */
    public List<JSONObject> getPurchaseOrderInfo(String platformOrderNo) {
        // 查询成品采购单
        List<JSONObject> purchaselist = DBUtil.select("Purchase-查询成品采购单号", new ContentDTO("", platformOrderNo));
        // 查询oem采购单
        List<JSONObject> oemPurchaselist = DBUtil.select("Purchase-查询OEM采购单号", new ContentDTO("", platformOrderNo));

        for (JSONObject ob : oemPurchaselist) {
            JSONObject purchaseOrder = new JSONObject();
            String purchase_no = ob.getString("oem_purchase_no");
            String purchase_status = ob.getString("purchase_status");
            String wareHouse = ob.getString("ware_code");
            purchaseOrder.put("purchase_no",purchase_no);
            purchaseOrder.put("purchase_status",purchase_status);
            purchaseOrder.put("ware_code",wareHouse);
            purchaselist.add(purchaseOrder);

        }

        // 没有查到采购单号报错，中断流程
        if (purchaselist.isEmpty()) {
            LogUtil.warn("没有查到对应的采购单号！！！");
        }

        LogUtil.warn("【采购】获取采购单号：" + purchaselist);

        return purchaselist;
    }
}
